package im.dhc.gitdiff;

import im.dhc.gitdiff.common.DiffFile;
import im.dhc.gitdiff.util.*;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.diff.DiffEntry;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author songhaichao
 * @create 2020-07-13-10:41
 **/
public class GitFtl {

    public static void main(String[] args) throws Exception{
        String newVersion = "c6fd68e78845656998d68cf64c8a34fd7cf02abe";
        String oldVersion = "baa0848a8faba137eb7f0ea82df702b261a5b998";//

        Git git = GitUtil.gitInit();

        List<DiffEntry> diffs = GitUtil.diffTwoBranch(git, GitUtil.getLocalBranchRef("develop"), GitUtil.getRemoteBranchRef("develop-weipeisong"));

        //GitUtil.diffTwoVersion(git, newVersion, oldVersion);
        //GitUtil.diffHeadAndVersion(git, oldVersion);

        List<DiffFile> backupList = new ArrayList<DiffFile>();
        List<DiffFile> updateList = new ArrayList<DiffFile>();
        Map<String,String> moduleMap = new HashMap<String,String>();
        for (DiffEntry diff : diffs) {
            String changeType = diff.getChangeType().name();
            System.out.println("-----changeType:" + changeType + "-----");
            System.out.println("-----oldPath:[" + diff.getOldPath() + "]newPath:[" + diff.getNewPath() + "]-----");

            DiffFile diffInfo = new DiffFile(diff);

            if (diffInfo.getPath() != null && FilePathFilter.filter(diffInfo.getPath())) {
                if (PathUtil.isJavaFile(diff.getNewPath())) {
                    String module = PathUtil.getModule(diff.getNewPath());
                    if (moduleMap.containsKey(module)) {
                        continue;
                    } else {
                        moduleMap.put(module, "Y");
                    }
                    backupList.add(diffInfo);
                }else {
                    if ("MODIFY".equals(changeType)) {
                        backupList.add(diffInfo);
                    }
                }
                updateList.add(diffInfo);
                diffInfo.copyFile();
            }
        }

        Map<String,Object> data = new HashMap<String, Object>();
        data.put("backupList",backupList);
        data.put("updateList",updateList);

        PropertityUtil propertityUtil = new PropertityUtil();
        FreemarkerUtil.makeShell(propertityUtil.getProperty("shellDir"),data);
    }
}
